﻿// Copyright Microsoft

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Samples.SqlServer.Common;
using Microsoft.Samples.SqlServer.Activities;
using System.Data.SqlClient;
using System.Net;

namespace Microsoft.Samples.SqlServer.Common.Forms
{
    public partial class DesignerForm : Form
    {
        public string ClientName {get; set;}
        private int XPos;
        private int YPos;

        public DesignerForm()
        {
            InitializeComponent();          
        }

        public string WorkflowPath
        {
            get
            {
                return workflowDesignerControl.WorkflowPath;
            }
        }

        void activitiesForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            activitiesButton.Enabled = true;
        }

        private void DesignerForm_Load(object sender, EventArgs e){ }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.Left = this.Left - (XPos - e.X);
                this.Top = this.Top - (YPos - e.Y);
            }
        }

        private void Panel_MouseDown(object sender, MouseEventArgs e)
        {
            XPos = e.X;
            YPos = e.Y;
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            workflowDesignerControl.ClientName = this.ClientName;
            workflowDesignerControl.New();
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            DialogResult result = openDialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                string workflowPath = openDialog.FileName;
                workflowDesignerControl.ClientName = this.ClientName;
                workflowDesignerControl.Load(workflowPath);
            }
        }

        private void activitiesButton_Click(object sender, EventArgs e)
        {
            ActivitiesForm activitiesForm = new ActivitiesForm();
            activitiesForm.FormClosed += activitiesForm_FormClosed;
            activitiesForm.Top = 100;
            activitiesForm.Left = 100;
            activitiesForm.Show(new MyWin32Window(this.Handle));
            activitiesButton.Enabled = false;
        }

        private void captionPanel_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
                this.WindowState = FormWindowState.Maximized;
            else
                this.WindowState = FormWindowState.Normal;
        }

    }

    /// <summary>
    /// This class can be used to show a Toolform as a child window.
    /// For example, form.Show(new MyWin32Window(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle));
    /// </summary>
    public class MyWin32Window : System.Windows.Forms.IWin32Window
    {
        private IntPtr _hwnd;

        public MyWin32Window(IntPtr handle)
        {
            _hwnd = handle;
        }

        public IntPtr Handle
        {
            get
            {
                return _hwnd;
            }
        }
    }
}
